<template>
  <div>
    <pannel title="管理员信息">
      <p>管理员ID:{{ accountInfo.id }}</p>
      <el-divider></el-divider>
      <p>账号:{{ accountInfo.account }}</p>
      <el-divider></el-divider>
      <p>用户组:{{ accountInfo.userGroup }}</p>
      <el-divider></el-divider>

      <p>创建时间:{{ accountInfo.ctime }}</p>
      <el-divider></el-divider>

      <div class="dpflex">
        <div>
            头像
        </div>

        <el-upload
          class="avatar-uploader"
          :action="acitonUrl"
          :show-file-list="false"
          :on-success="successFile"
          :before-upload="beforefile"
          drag
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <el-divider></el-divider>
    </pannel>
  </div>
</template>

<script>
import Pannel from "@/components/Pannel.vue";
import { userInfo } from "@/api/users";
export default {
  data() {
    return {
      accountInfo: "",
      imageUrl:"",
        acitonUrl:'',
        baseUrl:'http://127.0.0.1:5000'
    };
  },
  components: {
    Pannel,
  },
  methods: {
    async getUserInfo() {
      let { id } = JSON.parse(localStorage.getItem("user"));
      let res = await userInfo({ id });
      this.accountInfo = res.accountInfo;
      this.imageUrl = this.accountInfo.imgUrl
    },

    //上传事件
    successFile(res,file,fileList){
        if(res.code ==0){
            this.$message.success('上传成功');
            //通知 顶部组件 更新用户的新头像
              this.$bus.$emit('changeUserInfo','')
              //重新渲染页面
              this.getUserInfo()
        }
    },
    //上传之前 --用户文件限制
    beforefile(file){
        //文件大小 不得超过200K
        let size = file.size
     
        if(size>=200*1024){
            //取消上传
            this.$message.error('上传文件太大了...')
            return false;
        }
        //文件类型 必须是 图片
        //特殊情况 空的type 直接提示 
        let types = 'image/png,image/jpeg,image/jpg,image/gif,image/svg,image/webp'
        let type =  file.type;
        if(type ==''){
             this.$message.error('文件类型出错...')
              return false;
        }
        if(!types.includes(type)){
            this.$message.error('文件类型出错...')
            return false;
        }
    }
  },
  created() {
    this.getUserInfo();
      let id = JSON.parse(localStorage.getItem('user')).id
    this.acitonUrl = this.baseUrl+'/users/avatar_upload?id='+id;
  },
};
</script>

<style lang="less" scoped>
 .avatar-uploader .el-upload {

    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
        border: 1px dashed #d9d9d9;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>